<?php
namespace app\admin\controller;

use app\admin\model\Node;
use app\admin\model\AuthGroup;
use think\Db;

class Role extends Base
{
    /**
     * [index 角色列表]
     *
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function index()
    {

        if(request()->isPost()){
            $key = input('key');
            $map = [];
            if($key){
                $map['title'] = ['like', "%" . $key . "%"];
            }
            $user  = new AuthGroup();
            $page  = input('page', 1);
            $limit = input('limit', 10);
            $count = $user->getAllRole($map);  //总数据
            $lists = $user->getRoleByWhere($map, $page, $limit);
            return json(['code' => 0, 'data' => $lists, 'count' => $count, 'msg' => '角色列表']);
        }
        return $this->fetch();
    }

    /**
     * [roleAdd 添加角色]
     *
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function roleAdd()
    {
        if(request()->isAjax()){

            $param = input('post.');
            $role  = new AuthGroup();
            $flag  = $role->insertRole($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        return $this->fetch();
    }

    /**
     * [roleEdit 编辑角色]
     *
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function roleEdit()
    {
        $role = new AuthGroup();
        if(request()->isAjax()){
            $param = input('post.');
            $flag  = $role->editRole($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign([
            'role' => $role->getOneRole($id)
        ]);
        return $this->fetch();
    }

    /**
     * [roleDel 删除角色]
     *
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function roleDel()
    {
        $id   = input('param.id');
        $role = new AuthGroup();
        $flag = $role->delRole($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

    /**
     * [role_state 用户状态]
     *
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function role_state()
    {
        $id     = input('param.id');
        $status = Db::name('auth_group')->where('id', $id)->value('status');//判断当前状态情况
        if($status == 1){
            $flag = Db::name('auth_group')->where('id', $id)->setField(['status' => 0]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已禁止']);
        }else{
            $flag = Db::name('auth_group')->where('id', $id)->setField(['status' => 1]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已开启']);
        }
    }

    /**
     * [giveAccess 分配权限]
     *
     * @return [type] [description]
     * @author [田建龙] [864491238@qq.com]
     */
    public function giveAccess()
    {
        $param = input('param.');
        $node  = new Node();
        //获取现在的权限
        if('get' == $param['type']){
            $nodeStr = $node->getNodeInfo($param['id']);
            return json(['code' => 1, 'data' => $nodeStr, 'msg' => 'success']);
        }
        //分配新权限
        if('give' == $param['type']){

            $doparam = [
                'id'    => $param['id'],
                'rules' => $param['rule']
            ];
            $user    = new AuthGroup();
            $flag    = $user->editAccess($doparam);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
    }
}